﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enter.Mvc.Base.Controllers;
using Enter.Services.Mail;
using Enter.Services.DataCache;
using System.Web.Mvc;
using Enter.Entity;
using Enter.Mvc.Mall.Model.Product;
using Enter.Config;
using Enter.Common;
using System.ComponentModel;
using Enter.Mvc.Mall.Model.User;
using Enter.Mvc.Mall.Model;
using System.Data;

namespace Enter.Mvc.Mall.Controllers
{
    public class ProductController : BaseControllers
    {
        private readonly ICache icache;
        private readonly IMail imail;

        public ProductController(ICache cache, IMail mail)
        {
            this.icache = cache;
            this.imail = mail;
        
        }

        #region

        enum NavType
        { 
            /// <summary>
            /// 默认样式
            /// </summary>
            spu_cat= 0,
            /// <summary>
            /// 属性搜索页
            /// </summary>
            spu_pro=1,
            spu_brand
            
        }
       
       

        
        /// <summary>
        /// 热门店铺排行
        /// </summary>
        /// <param name="cNo"></param>
        /// <returns></returns>
        public ActionResult HotShopsRank(string cNo)
        {

            return PartialView();

        }
       
          /// <summary>
        /// 商品热销排行
        /// </summary>
        /// <param name="cNo"></param>
        /// <returns></returns>
        public ActionResult HotProductsRank()
        {

            List<ProductModel> list=new List<ProductModel>();
            List<ProductModel> list1 = new List<ProductModel>();
            string CateNO = RequestStrHelper.GetStringFromRequest("cat");// "01010201";
            string areaNo = UserArea.GetAreaNO();
            if (RequestStrHelper.GetStringFromRequest("nav") == NavType.spu_cat.ToString())
            {
                List<T101_GoodsEntity> goodslist = imail.GetCommentGoodsbyCID(areaNo, CateNO, 6);
                if (list != null)
                {
                    foreach (T101_GoodsEntity entity in goodslist)
                    {
                        ProductModel model = new ProductModel();
                        model.GID = entity.GID;
                        model.Title = entity.GTitle;
                        model.ImgUrl = entity.ImgUrl;
                        model.CommentScore = entity.GGCommentScore;
                        model.CommentCount = entity.CommentCount;
                        list.Add(model);
                    }
                }
                List<T101_GoodsEntity> goodslist1 = imail.GetHotGoodsbyCID(areaNo, CateNO, 6);
                if (list != null)
                {
                    foreach (T101_GoodsEntity entity in goodslist1)
                    {
                        ProductModel model = new ProductModel();
                        model.GID = entity.GID;
                        model.Title = entity.GTitle;
                        model.ImgUrl = entity.ImgUrl;
                        model.CommentScore = entity.GGCommentScore;
                        model.CommentCount = entity.CommentCount;
                        list1.Add(model);
                    }
                }
                ViewData["show"] = "1";
            }
            else {
                ViewData["show"] = "0";
            }
            IDictionary<string, object> test = new Dictionary<string, object>();
            test.Add("T1", list);
            test.Add("T2", list1);
            return PartialView(test);

        }

        #region 属性
        public ActionResult catePropList()
        {

            string CateNO = RequestStrHelper.GetStringFromRequest("cat") ;
           
            List<T106_GoodClassPropertyEntity> plist = imail.GetClassPropertyList(CateNO);
            List<Property_Value> pvlist = null;

            #region 品牌
            Property_Value brank = GetBrank(CateNO);
            if (brank != null)
            {
                if (pvlist == null)
                {
                    pvlist = new List<Property_Value>();
                }
                pvlist.Add(brank);
            }

            #endregion
            #region 基本属性
            if (plist != null && plist.Count > 0)
            {
                if (pvlist == null)
                {
                    pvlist = new List<Property_Value>();
                }
                foreach (T106_GoodClassPropertyEntity p in plist)
                {
                    if (p.chooseType != Enter.Entity.T106_GoodClassPropertyEntity.EnumchooseType.Input)
                    {
                        Property_Value pv = new Property_Value();
                        PropertyModel pm = new PropertyModel();
                        pm.PNo = p.PNo;
                        pm.PName = p.PName;
                        pm.IsRequired = p.IsRequired;

                        pm.PorpertyType = EnumProductPorpertyType.Base;
                        pv.PropertyName = pm;


                        List<PropertyValueModel> vs = null;
                        List<T116_GoodClassProperty_ValueEntity> vlist = imail.GetPrpertyValue(pm.PNo);
                        if (vlist != null && vlist.Count > 0)
                        {
                            vs = new List<PropertyValueModel>();

                            foreach (T116_GoodClassProperty_ValueEntity v in vlist)
                            {
                                PropertyValueModel vm = new PropertyValueModel();
                                vm.VNO = v.VNO;
                                vm.VName = v.VName;
                                vm.PNo = v.PNo;
                                RequestHelper request = new RequestHelper(Request.QueryString);
                                request.Remove("currentIndex");
                                request.Set("nav", NavType.spu_pro.ToString());
                                vm.Href = request.SetAndGetString("prop", v.PNo+":"+v.VNO, RequestHelper.EnumUrl.append);
                                vs.Add(vm);
                            }
                            pv.PropertyValues = vs;
                        }
                        else
                        {
                            pv.PropertyValues = null;
                        }


                        pvlist.Add(pv);
                    }
                }
            }
            #endregion
            return PartialView(pvlist);
        }

        #endregion


        public ActionResult search_product(string  search)
        {
            ViewData["cat"] = RequestStrHelper.GetStringFromRequest("cat");
            string CateNO = RequestStrHelper.GetStringFromRequest("cat");// "01010201";
            string prop = RequestStrHelper.GetStringFromRequest("prop");
            string brand = RequestStrHelper.GetStringFromRequest("brand");
            string areaNo = UserArea.GetAreaNO();
            string searchkey = RequestStrHelper.GetStringFromRequest("key");
            decimal sprice = RequestStrHelper.GetDecimalFromRequest("start_price");
            decimal eprice = RequestStrHelper.GetDecimalFromRequest("end_price");
            int pagesize = 10;
            int currentIndex = RequestStrHelper.GetIntFromRequest("currentIndex", 1);
            int totalPages, totalCount;
            EnumGoodOrder orderCol;
            EnumOrderType orderType=EnumOrderType.Desc;
            string sort = RequestStrHelper.GetStringFromRequest("sort", EnumProductOrder.st.ToString());
           
             if (sort == EnumProductOrder.p.ToString())
            {
                orderCol = EnumGoodOrder.Price; 
                orderType = EnumOrderType.Asc;
            }
            else if (sort == EnumProductOrder.pd.ToString())
            {
                orderCol = EnumGoodOrder.Price;
               
            }
            else if (sort == EnumProductOrder.d.ToString())
            {
                orderCol = EnumGoodOrder.MonthSale;
               
            }
            else if (sort == EnumProductOrder.pt.ToString())
            {
                orderCol = EnumGoodOrder.AddTime;
              
            }
             else if (sort == EnumProductOrder.td.ToString())
             {
                 orderCol = EnumGoodOrder.Sale;

             }
             else {
                 
                     orderCol = EnumGoodOrder.SaleTime;
                 
             }
             string props = "";
             if (!string.IsNullOrEmpty(prop))
             {
                 string[] ps = prop.Split(';');
                 foreach (string p in ps)
                 {
                     props += string.Format("'{0}',", p.Split(':')[1]);
                 }
             }
             DataTable dt = imail.GetGoodsbyCID(areaNo, CateNO, props, searchkey, sprice, eprice, pagesize, currentIndex, orderCol, orderType, out totalPages, out totalCount);
             ViewData["currentIndex"] = currentIndex;
             ViewData["totalPages"] = totalPages;
           

    

            List<ProductModel> list=new List<ProductModel>();
              if (dt != null)
              {
                  foreach (DataRow dr in dt.Rows)
                  {
                      ProductModel model = new ProductModel();
                      model.GID = int.Parse(dr["GID"].ToString());
                      model.Title = dr["GTitle"].ToString();
                      model.ImgUrl = dr["ImgUrl"].ToString();
                      model.MonthSaleCount = SafeConvert.ToInt32(dr["MonthSaleCount"].ToString()); 
                      model.CommentCount = SafeConvert.ToInt32(dr["CommentCount"].ToString());
                      model.SalePRICE = SafeConvert.ToDecimal(dr["GSalePRICE"].ToString());
                      list.Add(model);
                  }
              }
              return View(list);

        }
        private Property_Value GetBrank(string CateNO)
        {
            Property_Value pv = null;
            List<T109_GoodBrandEntity> list = imail.GetGoodBrandbyCateNO(CateNO);

            if (list != null && list.Count > 0)
            {
                pv = new Property_Value();
                PropertyModel pm = new PropertyModel();
               
                pm.PName = "品牌";
                pm.IsRequired = true;
                pm.PorpertyType = EnumProductPorpertyType.Brand;
                pv.PropertyName = pm;
                List<PropertyValueModel> vs = new List<PropertyValueModel>();
                foreach (T109_GoodBrandEntity entity in list)
                {
                    PropertyValueModel vm = new PropertyValueModel();
                    vm.VNO = entity.BNO;
                    vm.VName = entity.BName;
                    RequestHelper request = new RequestHelper(Request.QueryString);
                    request.Remove("currentIndex");
                    request.Set("nav", NavType.spu_brand.ToString());
                    vm.Href = request.SetAndGetString("brand", entity.BNO, RequestHelper.EnumUrl.Replace);
                    //if (vm.VNO == bNo)
                    //{
                    //    vm.Selected = true;
                    //}
                    vs.Add(vm);
                }
                pv.PropertyValues = vs;
            }
            return pv;
        }
        
        #endregion

    }
}
